<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="安全级" prop="securityLevel">
        <el-input v-model="formData.securityLevel" placeholder="请输入安全级" />
      </el-form-item>
      <el-form-item label="子项目阶段" prop="subProjectPhase">
        <el-input v-model="formData.subProjectPhase" placeholder="请输入子项目阶段" />
      </el-form-item>
      <el-form-item label="导入分类" prop="importCategory">
        <el-input v-model="formData.importCategory" placeholder="请输入导入分类" />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input v-model="formData.category" placeholder="请输入分类" />
      </el-form-item>
      <el-form-item label="外部编码" prop="externalCode">
        <el-input v-model="formData.externalCode" placeholder="请输入外部编码" />
      </el-form-item>
      <el-form-item label="内部编码" prop="internalCode">
        <el-input v-model="formData.internalCode" placeholder="请输入内部编码" />
      </el-form-item>
      <el-form-item label="文件标题" prop="fileTitle">
        <el-input v-model="formData.fileTitle" placeholder="请输入文件标题" />
      </el-form-item>
      <el-form-item label="负责人" prop="personInCharge">
        <el-input v-model="formData.personInCharge" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="文件版本" prop="fileVersion">
        <el-input v-model="formData.fileVersion" placeholder="请输入文件版本" />
      </el-form-item>
      <el-form-item label="文件状态" prop="fileStatus">
        <el-radio-group v-model="formData.fileStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审查类型" prop="reviewType">
        <el-select v-model="formData.reviewType" placeholder="请选择审查类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="5W" prop="fiveW">
        <el-input v-model="formData.fiveW" placeholder="请输入5W" />
      </el-form-item>
      <el-form-item label="IED文件类别" prop="iedFileCategory">
        <el-input v-model="formData.iedFileCategory" placeholder="请输入IED文件类别" />
      </el-form-item>
      <el-form-item label="页数" prop="pageCount">
        <el-input v-model="formData.pageCount" placeholder="请输入页数" />
      </el-form-item>
      <el-form-item label="是否有版本号" prop="hasVersionNumber">
        <el-radio-group v-model="formData.hasVersionNumber">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否影响合同执行" prop="affectsContractExecution">
        <el-radio-group v-model="formData.affectsContractExecution">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否属于竣工文件" prop="isCompletionDocument">
        <el-radio-group v-model="formData.isCompletionDocument">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="计划提交日期" prop="plannedSubmissionDate">
        <el-date-picker
          v-model="formData.plannedSubmissionDate"
          type="date"
          value-format="x"
          placeholder="选择计划提交日期"
        />
      </el-form-item>
      <el-form-item label="形成日期" prop="formationDate">
        <el-date-picker
          v-model="formData.formationDate"
          type="date"
          value-format="x"
          placeholder="选择形成日期"
        />
      </el-form-item>
      <el-form-item label="实际提交日期" prop="actualSubmissionDate">
        <el-date-picker
          v-model="formData.actualSubmissionDate"
          type="date"
          value-format="x"
          placeholder="选择实际提交日期"
        />
      </el-form-item>
      <el-form-item label="通道号" prop="channelNumber">
        <el-input v-model="formData.channelNumber" placeholder="请输入通道号" />
      </el-form-item>
      <el-form-item label="要求答复日期" prop="requiredResponseDate">
        <el-date-picker
          v-model="formData.requiredResponseDate"
          type="date"
          value-format="x"
          placeholder="选择要求答复日期"
        />
      </el-form-item>
      <el-form-item label="实际答复日期" prop="actualResponseDate">
        <el-date-picker
          v-model="formData.actualResponseDate"
          type="date"
          value-format="x"
          placeholder="选择实际答复日期"
        />
      </el-form-item>
      <el-form-item label="实际答复通道号" prop="actualResponseChannel">
        <el-input v-model="formData.actualResponseChannel" placeholder="请输入实际答复通道号" />
      </el-form-item>
      <el-form-item label="审查结果" prop="reviewResult">
        <el-input v-model="formData.reviewResult" placeholder="请输入审查结果" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DesignIedApi, DesignIedVO } from '@/api/datastore/designied'

/** IED设计文件管理 表单 */
defineOptions({ name: 'DesignIedForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  securityLevel: undefined,
  subProjectPhase: undefined,
  importCategory: undefined,
  category: undefined,
  externalCode: undefined,
  internalCode: undefined,
  fileTitle: undefined,
  personInCharge: undefined,
  fileVersion: undefined,
  fileStatus: undefined,
  reviewType: undefined,
  fiveW: undefined,
  iedFileCategory: undefined,
  pageCount: undefined,
  hasVersionNumber: undefined,
  affectsContractExecution: undefined,
  isCompletionDocument: undefined,
  plannedSubmissionDate: undefined,
  formationDate: undefined,
  actualSubmissionDate: undefined,
  channelNumber: undefined,
  requiredResponseDate: undefined,
  actualResponseDate: undefined,
  actualResponseChannel: undefined,
  reviewResult: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DesignIedApi.getDesignIed(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DesignIedVO
    if (formType.value === 'create') {
      await DesignIedApi.createDesignIed(data)
      message.success(t('common.createSuccess'))
    } else {
      await DesignIedApi.updateDesignIed(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    securityLevel: undefined,
    subProjectPhase: undefined,
    importCategory: undefined,
    category: undefined,
    externalCode: undefined,
    internalCode: undefined,
    fileTitle: undefined,
    personInCharge: undefined,
    fileVersion: undefined,
    fileStatus: undefined,
    reviewType: undefined,
    fiveW: undefined,
    iedFileCategory: undefined,
    pageCount: undefined,
    hasVersionNumber: undefined,
    affectsContractExecution: undefined,
    isCompletionDocument: undefined,
    plannedSubmissionDate: undefined,
    formationDate: undefined,
    actualSubmissionDate: undefined,
    channelNumber: undefined,
    requiredResponseDate: undefined,
    actualResponseDate: undefined,
    actualResponseChannel: undefined,
    reviewResult: undefined,
  }
  formRef.value?.resetFields()
}
</script>